package com.itheima.excel2;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WriteExcelDemo01 {
    public static void main(String[] args) {

        WriteCellStyle cellStyle = new WriteCellStyle();
        cellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置字体居中
        //设置字体样式
        WriteFont font = new WriteFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)12);
        cellStyle.setWriteFont(font);
        HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(cellStyle, cellStyle);



        EasyExcel.write(new File("D:/4.xls"))
                .excelType(ExcelTypeEnum.XLS)

                .registerWriteHandler(styleStrategy)//设置字体 , 居中样式
                .registerWriteHandler(new CustomStyle())//设置自定义样式 ---> 列宽

                .head(headers())
                .sheet("笑傲江湖")
                .doWrite(getData());


    }

    private static List<List<String>> getData() {
        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<String> rowList = new ArrayList<>();
            rowList.add(i+"");
            rowList.add("Tom"+i);
            rowList.add(i+"");
            rowList.add("男");
            rowList.add("ITHEIMA");
            rowList.add("缺卡/缺卡");
            rowList.add("缺卡/迟到");

            dataList.add(rowList);
        }
        return dataList;
    }

    private static List<List<String>> headers() {
        List<List<String>> headers = new ArrayList<>();

        headers.add(Arrays.asList("编号"));
        headers.add(Arrays.asList("姓名"));
        headers.add(Arrays.asList("年龄"));
        headers.add(Arrays.asList("性别"));
        headers.add(Arrays.asList("备注"));
        headers.add(Arrays.asList("2021-04-01"));
        headers.add(Arrays.asList("2021-04-02"));

        return headers;
    }
}
